﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BYFW.BOL;
using System.Data;

namespace LDSP.BRL
{
    public class EnterpriseConfig : BaseEntity
    {
        public EnterpriseConfig(int enterpriseID)
            : this()
        {
            m_enterpriseID = enterpriseID;
        }
        protected EnterpriseConfig()
        {
            m_tableName = "t_enterpriseConfig";
            m_hasComment = true;
            m_hasName = true;
            m_tblCacheManager.IsEnable = true;
		}

		protected int m_enterpriseID = 0;

		/// <summary>
		/// 企业ID
		/// </summary>
		public int EnterpriseID
		{
			get { return m_enterpriseID; }
		}

		protected override void FillContainer()
		{
			base.FillContainer();
			m_container.Add("enterpriseID", m_enterpriseID);
		}

		public override bool GetProperties(System.Data.DataRow record)
		{
			if(!base.GetProperties(record))
			{
				return false;
			}
			m_enterpriseID = this.GetIntValue(record, "enterpriseID");
			return true;
		}

        /// <summary>
        /// 获取指定的Config，若其不存在，则使用默认值创建一个
        /// </summary>
        /// <param name="enterpriseID">企业的ID</param>
        /// <param name="code">指定的Config名</param>
        /// <param name="createNew">当config不存在时，则创建一个</param>
        /// <param name="dft">当config不存在时，使用此默认值创建一个</param>
        /// <returns></returns>
        protected EnterpriseConfig GetConfig(string name, bool createNew = true, object dft = null)
        {
            string cond = string.Format("where {0}={1} and {2}={3}",
                this.DB.GetCol("enterpriseID"),
                this.DB.GetVal(m_enterpriseID),
                this.DB.GetCol("name"),
                this.DB.GetVal(name));
            DataTable dt = this.GetRecords(cond, true);
            EnterpriseConfig tmp = new EnterpriseConfig();
            if (dt.Rows.Count > 0)
            {
                tmp.GetProperties(dt.Rows[0]);
            }
            else if (createNew)
            {
                tmp.m_enterpriseID = m_enterpriseID;
                tmp.Name = name;
                if (dft != null)
                {
                    tmp.Comment = dft.ToString();
                }
                tmp.Add();
            }
            return tmp;
        }

        /// <summary>
        /// 移除指定的Config
        /// </summary>
        /// <param name="enterpriseID">企业的ID</param>
        /// <param name="code">指定的Config名</param>
        public void RemoveConfig(string name)
        {
            EnterpriseConfig tmp = this.GetConfig(name, false);
            tmp.Delete();
        }

        #region System Config

        /// <summary>
        /// 实施措施名
        /// </summary>
        public string ActionFieldName
        {
            get
            {
                string res = GetConfig("ActionFieldName").Comment;
                return string.IsNullOrEmpty(res) ? "实施措施" : res;
            }
            set
            {
                if (this.ActionFieldName != value)
                {
                    EnterpriseConfig tmp = GetConfig("ActionFieldName");
                    tmp.Comment = value;
                    tmp.Update();
                }
            }
        }
        /// <summary>
        /// 自我评估名
        /// </summary>
        public string SelfEvaluationFieldName
        {
            get
            {
                string res = GetConfig("SelfEvaluationFieldName").Comment;
                return string.IsNullOrEmpty(res) ? "自我评估" : res;
            }
            set
            {
                if (this.SelfEvaluationFieldName != value)
                {
                    EnterpriseConfig tmp = GetConfig("SelfEvaluationFieldName");
                    tmp.Comment = value;
                    tmp.Update();
                }
            }
        }
        #endregion
    }
}
